package com.btcode.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Pattern;

public class PassWordUtil {

    public static void main(String[] args){
        System.out.println(PassWordUtil.isStrong("1q2w3e"));
    }

    /**
     * 弱口令正则，不满足这些的就是弱口令，后续根据需要增加
     */
    private static final String[] PASSWORD_PATTERN = {".*[a-zA-Z]+.*",".*[0-9]+.*",".{6,}"};

    /***密码校验，最少8位，包括至少1个小写字母，1个数字，1个特殊字符 !@#$%^&*?.()
     *
     */
    private static final String[] NEW_PASSWORD_PATTERN = {".*[a-zA-Z]+.*",".*[0-9]+.*",".{8,}",".*[!@#$%^&*?.()]+.*"};


    /**
     * 典型的弱口令
     */
    private static final String[] DICTIONARY = {"password","123456", "abc123", "iloveyou", "adobe123", "123123", "sunshine", "qweasd", "admin", "passwd"};

    /**
     * 判断密码强度是否符合要求
     * @param password
     * @return
     */
    public static boolean isStrong(String password){

        for (String simplePW:DICTIONARY) {
            if (password.equals(simplePW) ) {
                return false;
            }

            //如果密码长度跟简单密码相近，则判断是否以简单密码开头或者结尾
            if(password.length() - simplePW.length() < Integer.parseInt("3") && password.length() - simplePW.length() >= 0){
                if(password.indexOf(simplePW)==0 || password.indexOf(simplePW) == password.length()-simplePW.length()){
                    return false;
                }
            }
        }

        for (String pattern : PASSWORD_PATTERN) {
            if (!Pattern.matches(pattern, password)) {
                return false;
            }
        }

        return true;
    }


    /**
     * 判断密码强度是否符合要求
     * @param password
     * @return
     */
    public static boolean isOtherStrong(String password){

        for (String simplePW:DICTIONARY) {
            if (password.equals(simplePW) ) {
                return false;
            }

            //如果密码长度跟简单密码相近，则判断是否以简单密码开头或者结尾
            if(password.length() - simplePW.length() < Integer.parseInt("3")  && password.length() - simplePW.length() >= 0){
                if(password.indexOf(simplePW)==0 || password.indexOf(simplePW) == password.length()-simplePW.length()){
                    return false;
                }
            }
        }

        for (String pattern : NEW_PASSWORD_PATTERN) {
            if (!Pattern.matches(pattern, password)) {
                return false;
            }
        }

        return true;
    }

    /**
     * 密码md5加密,加盐方式：账号 + # + 密码
     * 1、读取配置，看是否需要md5加密
     * @param account
     * @param passWord
     * @return
     */
    public static String encryptPassWordByMd5(String account,String passWord) {
        //读取配置，看是否需要md5加密
        String isMd5PassWord = PropertiesUnit.getValue("isMd5PassWord");
        if(!"true".equals(isMd5PassWord)){
            return passWord;
        }
        String result = "";
        try {
            passWord = account + "#" + passWord;
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(passWord.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuilder buf = new StringBuilder();
            for (byte b1 : b) {
                i = b1;
                if (i < 0) {
                    i += Integer.valueOf("256");
                }
                if (i < Integer.valueOf("32")) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
            }
            result = buf.toString().toUpperCase();
        }
        catch (NoSuchAlgorithmException e) {
            System.out.println(e);
        }
        return result;
    }



}
